//
//  MainWindowController.m
//  Morsenguer
//
//  Created by Paúl Silva on 08/05/09.
//  Copyright 2009 Ixaya. All rights reserved.
//

#import "MainWindowController.h"


@implementation MainWindowController
@synthesize writtenString, traducedString;

- (id) init
{
	self = [super init];
	if (self != nil) {
		traducedString = @"";
		sonidosEnEspera = [NSMutableArray new]; //creamos un array mutable (osea que se puede modificar)

	}
	return self;
}
-(void)awakeFromNib{
	NSImage *imageOFF = [NSImage imageNamed:@"apagado.png"];
	[luzView setImage:imageOFF];
}


-(IBAction)morseIt:(id)sender{
	
	[morseItButton setEnabled:NO];
	[self setTraducedString:@""];
	NSString *word = self.writtenString;
	NSUInteger length = [word length];
	NSUInteger index = 0;
	
	NSLog(@"morseando: %@", word);
	while(index < length)
	{

		unichar letraChar = [word characterAtIndex:index];
		
//		comente esta linea porque no necesitas convertirla en string para compararla :D
//		NSString *letra = [NSString stringWithCharacters: &letraChar length:1];		
		
		[self traducir:letraChar];
		index ++;
	}
	
	// reproducimos porque ya terminamos de traducir el string
	[self textoParaAudios];
	[self reproduce];
}
-(void)traducir:(unichar)letra{
	
	// usamos un unichar porque los caracteres tienen un valor hexadecimal, decimal y octal
	// así podemos uar un switch en lugar de muchos ifs ya que puede ser decimal.
	
	NSString *appendString = @"";
	switch (letra) {
		case 97: // a
			appendString = @"·-";
			break;
		case 98: // b
			appendString = @"-···";
			break;
		case 99: // c
			appendString = @"-·-·";
			break;
		case 100: // d
			appendString = @"-··";
			break;
		case 101: // e
			appendString = @"·";
			break;
		case 102: // f
			appendString = @"··-·";
			break;
		case 103: // g
			appendString = @"--·";
			break;
		case 104: // h
			appendString = @"····";
			break;
		case 105: // i
			appendString = @"··";
			break;
		case 106: // j
			appendString = @"·---";
			break;
		case 107: // k
			appendString = @"-·-";
			break;
		case 108: // l
			appendString = @"·-··";
			break;
		case 109: // m
			appendString = @"--";
			break;
		case 110: // n
			appendString = @"-·";
			break;
		case 111: // o
			appendString = @"---";
			break;
		case 112: // p
			appendString = @"·--·";
			break;
		case 113: // q
			appendString = @"--·-";
			break;
		case 114: // r
			appendString = @"·-·";
			break;	
		case 115: // s
			appendString = @"···";
			break;
		case 116: // t
			appendString = @"-";
			break;
		case 117: // u
			appendString = @"··-";
			break;
		case 118: // v
			appendString = @"···-";
			break;
		case 119: // w
			appendString = @"·--";
			break;
		case 120: // x
			appendString = @"-··-";
			break;
		case 121: // y
			appendString = @"-·--";
			break;
		case 122: // z
			appendString = @"--··";
			break;
		case 49: // 1
			appendString = @"·----";
			break;
		case 50: // 2
			appendString = @"··---";
			break;
		case 51: // 3
			appendString = @"···--";
			break;
		case 52: // 4
			appendString = @"····-";
			break;
		case 53: // 5
			appendString = @"·····";
			break;
		case 54: // 6
			appendString = @"-····";
			break;
		case 55: // 7
			appendString = @"--···";
			break;
		case 56: // 8
			appendString = @"---··";
			break;
		case 57: // 9
			appendString = @"----·";
			break;
		case 48: // 0
			appendString = @"-----";
			break;
		default:
			appendString = @" ";
			NSLog(@"Wololo, WayoWayo, Wololo, Gold Please!, Wood Please!");
			break;
	}
	
	
	self.traducedString = [self.traducedString stringByAppendingString:appendString];
}

-(void)textoParaAudios {
	

	NSString *word = self.traducedString;
	NSUInteger length = [word length];
	NSUInteger index = 0;

	// esto agrega un sonido
	while(index < length)
	{
		unichar letraChar = [word characterAtIndex:index];
		NSString *letra = [NSString stringWithCharacters: &letraChar length:1];		

		NSSound *sonido = nil;
		if([letra isEqualToString:@"-"])
		{
			sonido = [NSSound soundNamed:@"largo.mp3"];
		} 
		else if([letra isEqualToString:@"·"]) 
		{
			sonido = [NSSound soundNamed:@"corto.mp3"];
		}
		
		//descomenta esta linea cuando agregues el audio, sino va a tronar (:D:D:D)
		if(sonido != nil)
		{
			[sonidosEnEspera addObject:sonido];
			[sonido setDelegate:self];
		}
		index++;
	}
}
-(void)reproduce{
	if([sonidosEnEspera count] > 0)
	{
		NSSound *sonido = [sonidosEnEspera objectAtIndex:0];
		[sonido play];
		[sonidosEnEspera removeObjectAtIndex:0];
	}
}
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying {

	// esto reproduce despues de que acabe el sonido anterior (es mejor que poner un sleep)
	if([sonidosEnEspera count] > 0)
	{
		[self reproduce];
		NSImage *imageOFF = [NSImage imageNamed:@"apagado.png"];
		[luzView setImage:imageOFF];
		[luzView2 setImage:imageOFF];
		[self performSelector:@selector(prenderLuz) withObject:nil afterDelay:0.1];
	}else{
		
	}
}

-(void)prenderLuz{
	NSImage *imageON = [NSImage imageNamed:@"encendido.png"];
	[luzView setImage:imageON];
	[luzView2 setImage:imageON];
}
@end
